package com.ruoyi.system.interAttr.attr;

import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.UUIDGenerator;
import com.ruoyi.system.interAttr.domain.GamesRes;
import com.ruoyi.system.interAttr.domain.InterAttrRes;
import com.ruoyi.system.interAttr.domain.InterServerRes;
import com.ruoyi.system.model.domain.SalePartner;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 密码游戏列表没有接口
 *
 * 等对面开发人员写好   2024 06 27
 *
 */
@Slf4j
@Service
public class YLDSAttr implements AdapterAttr {


    public static void main(String[] args) {
        Map<String, Object> params = new TreeMap<>();
        params.put("appId", "1240");
        params.put("gameId", "5");
        String cover = JSONObject.toJSONString(params)+"e3c8034bcfb0823cf328d4fd793a5468";
        String sign = DigestUtils.md5Hex(cover);
        String response = HttpRequest.post("https://b.yuebp.com/prod-api/open/commodity/gameInfo").header("user-key", sign)
                .body(JSONObject.toJSONString(params)).execute().body();
        System.out.println(response);
    }

    @Override
    public List<GamesRes> getGames(SalePartner salePartner) {
        Map<String, Object> params = new TreeMap<>();
        params.put("appId", "1240");
        String cover = JSONObject.toJSONString(params)+"e3c8034bcfb0823cf328d4fd793a5468";
        String sign = DigestUtils.md5Hex(cover);
        String result = HttpRequest.post("https://b.yuebp.com/prod-api/open/commodity/game").header("user-key", sign)
                .body(JSONObject.toJSONString(params)).execute().body();
        JSONObject resJson = JSONObject.parseObject(result);
        List<JSONObject> jsonObjects = JSONObject.parseArray(resJson.getString("data"), JSONObject.class);
        List<GamesRes> gamesResList = new ArrayList<>();

        for (JSONObject jsonObject : jsonObjects) {
            GamesRes gamesRes = new GamesRes();
            String id = jsonObject.getString("gameId");
            String title = jsonObject.getString("gameName");
            gamesRes.setId(id);
            gamesRes.setGameName(title);
            gamesResList.add(gamesRes);
        }
        return gamesResList;
    }

    @Override
    public List<InterServerRes> getServer(String gameId, SalePartner salePartner) {
        Map<String, Object> params = new TreeMap<>();
        params.put("appId", "1240");
        params.put("gameId", gameId);
        String cover = JSONObject.toJSONString(params)+"e3c8034bcfb0823cf328d4fd793a5468";
        String sign = DigestUtils.md5Hex(cover);
        String response = HttpRequest.post("https://b.yuebp.com/prod-api/open/commodity/zoneServer").header("user-key", sign)
                .body(JSONObject.toJSONString(params)).execute().body();

        JSONObject jsonObject = JSONObject.parseObject(response);
        List<JSONObject> data = JSONObject.parseArray(jsonObject.getString("data"), JSONObject.class);

        List<InterServerRes> interServerRes = new ArrayList<>();
        for (JSONObject datum : data) {
            InterServerRes interServer = new InterServerRes();
            interServer.setUuid(UUIDGenerator.generateUuid6());
            interServer.setId(datum.getString("zoneServerId"));
            interServer.setName(datum.getString("zoneServerName"));
            String servers = datum.getString("serverList");
            if(StringUtils.isNotEmpty(servers)) {
                List<InterServerRes> interServerRes1 = new ArrayList<>();
                List<JSONObject> jsonObjects = JSONObject.parseArray(servers, JSONObject.class);
                for (JSONObject object : jsonObjects) {
                    InterServerRes interServer1 = new InterServerRes();
                    String id1 = object.getString("zoneServerId");
                    String title1 = object.getString("zoneServerName");
                    interServer1.setUuid(UUIDGenerator.generateUuid6());
                    interServer1.setId(id1);
                    interServer1.setName(title1);
                    interServerRes1.add(interServer1);
                }
                interServer.setInterServerRes(interServerRes1);
            }
            interServerRes.add(interServer);
        }
        return interServerRes;
    }

    @Override
    public List<InterAttrRes> getPropety(String gameId, String type, SalePartner salePartner) {
        Map<String, Object> params = new TreeMap<>();
        params.put("appId", "1240");
        params.put("gameId", gameId);
        String cover = JSONObject.toJSONString(params)+"e3c8034bcfb0823cf328d4fd793a5468";
        String sign = DigestUtils.md5Hex(cover);
        String response = HttpRequest.post("https://b.yuebp.com/prod-api/open/commodity/gameInfo").header("user-key", sign)
                .body(JSONObject.toJSONString(params)).execute().body();

        List<InterAttrRes> interAttrResList = new ArrayList<>();
        JSONObject jsonObject = JSONObject.parseObject(response);
        List<JSONObject> data = JSONObject.parseArray(jsonObject.getString("data"), JSONObject.class);
        List<JSONObject> collect = new ArrayList<>();
        if (Objects.equals("1",type)){
            collect = data.stream().filter(d -> d.getString("fieldType").equals("radio")).collect(Collectors.toList());
        }else if  (Objects.equals("2",type)){
            collect = data.stream().filter(d -> d.getString("fieldType").equals("checkbox")).collect(Collectors.toList());
        }else if (Objects.equals("3",type)){
            collect = data.stream().filter(d -> d.getString("fieldType").equals("input")).collect(Collectors.toList());
        }
        for (JSONObject object : collect) {
            String string = object.getString("selectValueList");
            JSONArray jsonArray = JSONArray.parseArray(string);
            InterAttrRes interAttrRes = new InterAttrRes();
            interAttrRes.setUuid(UUIDGenerator.generateUuid6());
            interAttrRes.setId(object.getString("id"));
            interAttrRes.setName(object.getString("fieldName"));
            List<InterAttrRes> interAttrResList1 = new ArrayList<>();
            for(Object o : jsonArray) {
                InterAttrRes interAttrRes1 = new InterAttrRes();
                interAttrRes1.setUuid(UUIDGenerator.generateUuid6());
                interAttrRes1.setId(JSONObject.parseObject(o.toString()).getString("id"));
                interAttrRes1.setName(JSONObject.parseObject(o.toString()).getString("fieldValue"));
                interAttrResList1.add(interAttrRes1);
                interAttrRes.setInterServerRes(interAttrResList1);
            }
            interAttrResList.add(interAttrRes);
        }
        return interAttrResList;
    }
}